package com.so206.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.so206.mapper.SystemOrderMapper;
import com.so206.mapper.ext.OrderExtMapper;
import com.so206.po.SystemOrder;
import com.so206.po.ext.SystemOrderExt;
import com.so206.service.OrderService;
import com.so206.utils.BeanCheck;
import com.so206.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
@Transactional(rollbackFor = RuntimeException.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private SystemOrderMapper mapper;

    @Autowired
    private OrderExtMapper orderExtMapper;

    @Override
    public Long createOrder(SystemOrder order) {
        mapper.insert(order);
        return order.getId();
    }

    @Override
    public SystemOrder findOrderById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateOrder(SystemOrder order) {
        SystemOrder model = mapper.selectByPrimaryKey(order.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(order, model);
            mapper.updateByPrimaryKey(order);
        }
    }

    @Override
    public void deleteOrder(Long orderid) {
        mapper.deleteByPrimaryKey(orderid);
    }

    @Override
    public SystemOrder findOrderByOutOrderId(String id) {
        return mapper.findOrderByOutOrderId(id);
    }

    @Override
    public PageInfo<SystemOrder> findByPage(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<SystemOrder> users = mapper.selectOrderByClause(null, null);
        return new PageInfo<>(users);
    }

    @Override
    public PageInfo<SystemOrderExt> findByPage2(Integer page, Integer rows, Long uid, Integer status, Integer payType, String outOrderId, Integer order_type, Long oid) {
        PageHelper.startPage(page, rows);
        if (outOrderId != null && outOrderId.length() == 0) {
            outOrderId = null;
        }
        List<SystemOrderExt> users = orderExtMapper.findOrderWithUser(uid, status, outOrderId, payType, order_type, oid);
        return new PageInfo<>(users);
    }

    @Override
    public List<SystemOrder> findOrderByStatus(Integer status) {
        return mapper.selectOrderByClause(status, null);
    }

    @Override
    public List<SystemOrder> findOrderByStatus(Integer status, Integer sett) {
        return mapper.selectOrderByClause(status, sett);
    }

    @Override
    public Integer countOrderPayType(Integer payType, String timelike, String end_time) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(end_time));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Date start_time = utils.getStartTime();
        Date stop_time = utils.getEndTime();
        List<SystemOrder> orders = mapper.selectByPayTypeTimes(payType, start_time, stop_time);
        if (orders != null) {
            return orders.size();
        }
        return 0;
    }

    @Override
    public List<SystemOrder> findOrderList2(Integer status, Integer is_notify, Integer order_type) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 2);
        Date create_time = calendar.getTime();
        return mapper.selectByStatusIsNotifyType(status, is_notify, order_type, create_time);
    }

    @Override
    public PageInfo<SystemOrderExt> findByPage3(Integer page, Integer rows, Long uid, Integer status, Integer payType, String outOrderId, Integer order_type, Long oid) {
        PageHelper.startPage(page, rows);
        if (outOrderId != null && outOrderId.length() == 0) {
            outOrderId = null;
        }
        List<SystemOrderExt> users = orderExtMapper.findOrderWithUser2(uid, status, outOrderId, payType, order_type, oid);
        return new PageInfo<>(users);
    }

    @Override
    public SystemOrder find_orderByTradeNo(String to) {
        return mapper.find_orderByTradeNo(to);
    }

    @Override
    public void delete_old_data() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(System.currentTimeMillis()));
        calendar.add(Calendar.DATE, -2); //得到前一天
        Date create_time = calendar.getTime();
        mapper.deleteByCreateTime(create_time);
    }


}
